#!/usr/bin/env python3
# -*- coding:utf-8 -*-

from MOMA.MOMA_V1.uitls.bs4_v1.crawlers import Bs4_v1, bs4


class Bs4_v1_url(Bs4_v1):
    """
    爬取输入企业域名
    """

    def run_core(self):
        """
        执行任务
        :return:
        """
        bs4_obj = self.get_text()
        return self.get_url(bs4_obj)

    def get_url(self, get_url_obj: bs4.element.ResultSet) -> str | None:
        """
        爬取爱企查获取url
        :param get_url_obj:
        :return:
        """
        try:
            tmp_obj = get_url_obj.find_all(name="div", attrs={"class": "relate-info"})
            for obj in tmp_obj:
                url = obj.find_all(name="a", attrs={"rel": "nofollow"})
                if not url or url == "www.qcc.com":
                    continue
                url = url[0].get_text().strip()
                if url == "www.qcc.com":
                    continue
                self.url_list.append(url)
            return self.url_list[0]
        except IndexError as err:
            print("爱企查爬取不到url---{}".format(err))
